package Concurrent;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description：
 * Author: zhangc
 * Date：2017/9/23 15:30
 */
public class MyCountDownLatch {

    private static AtomicInteger atomicInteger = new AtomicInteger(0);
    private static final CountDownLatch c = new CountDownLatch(1);
    private static ExecutorService executors = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws InterruptedException {
        java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            executors.execute(new Th(latch));
        }
        long t = System.currentTimeMillis();
        c.countDown();
        latch.await();
        System.out.println("Time :" + (System.currentTimeMillis() - t));
        System.out.println("=====" + atomicInteger.get());
        executors.shutdown();

    }

    static class Th implements Runnable {
        private CountDownLatch countDownLatch;

        public Th(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            try {
                countDownLatch.countDown();
                c.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(atomicInteger.getAndIncrement());
        }
    }

}
